﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CVSummary : ICPRelationshipPage
{
    #region Properties
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Customer Visit - Visit Summary";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.CV_CARID] = null;
            Session[KSession.CV_STARTDATE] = null;
            Session[KSession.CV_ENDDATE] = null;
        }
        InitSummaryList();
    }
    protected void dgCVSum_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("FILTER"))
        {
            HandleFilter(e.Parameters);
        }
        else
        {
            HandleAll();
        }
    }
    
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    public void InitSummaryList()
    {
        String sxCarId =
            Session[KSession.CV_CARID] != null ?
            Session[KSession.CV_CARID].ToString() :
            String.Empty;
        DateTime dtmStart = Session[KSession.CV_STARTDATE] != null ?
            Convert.ToDateTime(Session[KSession.CV_STARTDATE]) :
            new DateTime();
        DateTime dtmEnd = Session[KSession.CV_ENDDATE] != null ?
            Convert.ToDateTime(Session[KSession.CV_ENDDATE]) :
            new DateTime();
        int iStartDate = dtmStart.Day;
        int iStartMonth = dtmStart.Month;
        int iStartYear = dtmStart.Year;
        int iEndDate = dtmEnd.Day;
        int iEndMonth = dtmEnd.Month;
        int iEndYear = dtmEnd.Year;
        Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
        DataTable dtVisitSummary = new DataTable();

        if (dtmStart != null && dtmEnd != null)
        {
            dtVisitSummary = cachedCV.GetVisitSummary(sxCarId, iStartDate, iStartMonth, iStartYear, iEndDate, iEndMonth, iEndYear);
        }

        dgCVSum.DataSource = dtVisitSummary;
        dgCVSum.DataBind();
    }
    public void HandleAll()
    {
        //Session[KSession.CV_CARID] = null;
        //Session[KSession.CV_STARTDATE] = null;
        //Session[KSession.CV_ENDDATE] = null;
        //InitSummaryList(true);

        //String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxCARId = String.Empty;
        DateTime dtmStart = new DateTime();
        DateTime dtmEnd = new DateTime();
        //if (!String.IsNullOrEmpty(sxSplitted[2]))
        //{
        //    dtmStart = Convert.ToDateTime(sxSplitted[2]);
        //}
        //if (!String.IsNullOrEmpty(sxSplitted[3]))
        //{
        //    dtmEnd = Convert.ToDateTime(sxSplitted[3]);
        //}
        Session[KSession.CV_CARID] = sxCARId;
        Session[KSession.CV_STARTDATE] = dtmStart;
        Session[KSession.CV_ENDDATE] = dtmEnd;
        InitSummaryList();
    }
    public void HandleFilter(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxCARId = String.Empty;
        DateTime dtmStart = DateTime.Now;
        DateTime dtmEnd = DateTime.Now;
        if (!String.IsNullOrEmpty(sxSplitted[1]))
        {
            sxCARId = sxSplitted[1];
        }
        if (!String.IsNullOrEmpty(sxSplitted[2]))
        {
            dtmStart = Convert.ToDateTime(sxSplitted[2]);
        }
        else
        {
            dtmStart = new DateTime();
        }
        if (!String.IsNullOrEmpty(sxSplitted[3]))
        {
            dtmEnd = Convert.ToDateTime(sxSplitted[3]);
        }
        else
        {
            dtmEnd = new DateTime();
        }
        Session[KSession.CV_CARID] = sxCARId;
        Session[KSession.CV_STARTDATE] = dtmStart;
        Session[KSession.CV_ENDDATE] = dtmEnd;
        InitSummaryList();
    }
    #endregion Custom Methods
}
